<template>
    <el-aside :width="isCollapse ? '64px' : '200px'">
            <div class="toggle-button" @click="toggleCollapse">
                <i :class="title"></i>
            </div>
            <el-menu
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409EFF"
                unique-opened
                :collapse="isCollapse"
                :collapse-transition="false"
                :router="true"
                :default-active="activePath"
            >
            <!-- default-active-当前激活菜单的 index -->
            <!-- 一级菜单 -->
            <!-- 
                menulist 通过Home页面传过来的数据去遍历
            -->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                    <!-- 一级菜单的模板区域 -->
                    <template slot="title">
                    <!-- 图标 -->
                    <i :class="iconsObj[item.id]"></i>
                    <!-- 文本 -->
                    <span>{{item.authName}}</span>
                    </template>
                    <!--  二级菜单的模板区域 -->
                    <el-menu-item 
                        :index="'/'+subItem.path" 
                        v-for="subItem in item.children" 
                        :key="subItem.id" 
                        @click="saveNavState('/'+subItem.path)" 
                    >
                        <template slot="title">
                            <!-- 图标 -->
                            <i class="el-icon-menu"></i>
                            <!-- 文本 -->
                            <span>{{subItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>
            </el-menu>      
    </el-aside>    
</template>

<script>
    export default {
        props:['menulist'],
        data(){
            return{
                // ico
                iconsObj:{
                    '125':'el-icon-service',
                    '103':'el-icon-ship',
                    '101':'el-icon-bicycle',
                    '102':'el-icon-umbrella',
                    '145':'el-icon-attract'
                },
                // 是否折叠
                isCollapse: false,
                title:'el-icon-turn-off',
                // 被激活的链接地址
                activePath:''
            }
        },
        created(){
            this.title = this.isCollapse ? 'el-icon-turn-off' : 'el-icon-open' 
            // 执行前
            this.activePath = window.sessionStorage.getItem('activePath')
        },
        methods:{
            // 点击按钮折叠与展开
            toggleCollapse(){
                this.isCollapse = !this.isCollapse
                this.title = this.isCollapse ? 'el-icon-turn-off' : 'el-icon-open' 
            },
            // 保存链接的激活状态
            saveNavState(activePath){
                // 向token中添加 activePath 传过来的路径
                window.sessionStorage.setItem('activePath',activePath)
                // 赋值
                this.activePath = activePath
            }
        },

    }
</script>

<style lang="less" scoped>
    .iconfont{
        margin-right: 10px;
    }
    .el-menu{
        border-right: none;
    }
    .toggle-button{
        background-color: #4a5064;
        font-size: 20px;
        line-height: 26px;
        color: #fff;
        text-align: center;
        cursor: pointer;
        color: antiquewhite;
    }
    .el-aside{
        background-color: #333744;
    }
</style>
